/**
 * 将原生提供的方法统一管理
 */
let GMM = window.gmm || {}
export default {
  getUserInfo() {
    try {
      return GMM.getUserInfo()
    } catch (e) {
      console.log(e + '不在APP内部')
    }
  },
  navigateBackTo({ value = 'login', ...restParams }) {
    try {
      console.log(JSON.stringify({ value, ...restParams }))
      GMM.navigateBackTo(JSON.stringify({ value, ...restParams }))
    } catch (e) {
      console.log(e + '不在APP内部')
    }
  },

  closeWebView() {
    try {
      GMM.closeWebView()
    } catch (e) {
      console.log(e + '不在APP内部')
    }
  },
  setNavigationBarVisible({ value = '' }) {
    try {
      GMM.setNavigationBarVisible(value)
    } catch (e) {
      console.log(e + '不在APP内部')
    }
  },
  getLocation(callback) {
    // 生成唯一的回调函数名
    const callbackName = 'locationCallback_' + Date.now()

    // 将回调函数临时挂载到window对象上
    window[callbackName] = function (data) {
      // 调用完成后删除临时函数
      delete window[callbackName]
      // 执行回调
      callback(data)
    }

    try {
      if (window.gmm && window.gmm.getLocation) {
        // 调用Android方法，传入回调函数名
        window.gmm.getLocation(callbackName)
      } else {
        console.error('gmm interface not available')
        delete window[callbackName]
      }
    } catch (e) {
      console.error('Error calling gmm.getLocation:', e)
      delete window[callbackName]
    }
  },
  // 拨打电话
  phoneFunc({ value = '' }) {
    try {
      GMM.phoneFunc(value)
    } catch (e) {
      console.log(e + '不在APP内部')
    }
  }
}
